﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using MaxStudio.Concrete.Web;
using MaxStudio.Concrete.DBUtility;
using MaxStudio.Concrete.Server;
using MaxStudio.Concrete.Server.Service;
using MaxStudio.Concrete.Server.Dao;
using System.Configuration;
using MaxStudio.Concrete.Component;
using System.Collections;

namespace MaxStudio.Concrete.Web.Pages
{
    public partial class RPTMixingProportionOfConcrete : BasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                BindGridView();
            }
        }
        #region Private Method


        private void BindGridView()
        {
            RPTMixingProportionOfConcreteOrderService RPTMixingProportionOfConcreteOrderServiceObj = ServiceFactory.getRPTMixingProportionOfConcreteOrderService();
            OperationResult<List<tb_RPT_MixingProportionOfConcreteOrder>> result = RPTMixingProportionOfConcreteOrderServiceObj.GetAllMixingProportionOfConcreteOrder();

            if (result.operationSuccess)
            {
                gvRPTMixingProportionOfConcreteList.DataSource = result.obj;
                gvRPTMixingProportionOfConcreteList.DataBind();
            }
            else
            {
                //EXCEPTION
                //return null;
            }
        }

        private void BindEdieDialog(int id)
        {
            RPTMixingProportionOfConcreteOrderService RPTMixingProportionOfConcreteOrderServiceObj = ServiceFactory.getRPTMixingProportionOfConcreteOrderService();
            OperationResult<tb_RPT_MixingProportionOfConcreteOrder> resultEdit = RPTMixingProportionOfConcreteOrderServiceObj.GetMixingProportionOfConcreteOrderById(id);

            if (resultEdit.operationSuccess == true)
            {
                txtEditInputDate.Text = resultEdit.obj.InputDate;
                txtEditProjectName.Text = resultEdit.obj.ProjectName;
                txtEditConstructionUnit.Text = resultEdit.obj.ConstructionUnit;
                txtEditEngineeringSite.Text = resultEdit.obj.EngineeringSite;
                txtEditStrengthGrade.Text = resultEdit.obj.StrengthGrade;
                txtEditSlump.Text = resultEdit.obj.Slump;
                txtEdit5Stones.Text = resultEdit.obj._5Stones;
                txtEdit31Stones.Text = resultEdit.obj._31Stones;
                txtEdit1Cement.Text = resultEdit.obj._1Cement;
                txtEdit3Cement.Text = resultEdit.obj._3Cement;
                txtEditFine.Text = resultEdit.obj.Fine;
                txtEditCoarseSand.Text = resultEdit.obj.CoarseSand;
                txtEditASH.Text = resultEdit.obj.ASH;
                txtEdit1Admixture.Text = resultEdit.obj._1Admixture;
                txtEdit2Admixture.Text = resultEdit.obj._2Admixture;
                txtEditWater.Text = resultEdit.obj.Water;
                txtEditRemark.Text = resultEdit.obj.Remark;
                txtEditApproval.Text = resultEdit.obj.Approval;
                txtEditPlan.Text = resultEdit.obj.Plan;

                lbLastModier.Text = "TEST";
                lbEidtId.Text = resultEdit.obj.Id.ToString();
                lbLastModifyDate.Text = resultEdit.obj.ModifyDate.ToShortDateString();
            }
        }

        private void DeleteRecords(int id)
        {
            lbDeleteId.Text = id.ToString();
        }

        #endregion

        #region Event
        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            RPTMixingProportionOfConcreteOrderService RPTMixingProportionOfConcreteOrderServiceObj = ServiceFactory.getRPTMixingProportionOfConcreteOrderService();
            tb_RPT_MixingProportionOfConcreteOrder instance = new tb_RPT_MixingProportionOfConcreteOrder();

            instance.InputDate = txtInputDate.Text;
            instance.ProjectName = txtProjectName.Text;
            instance.ConstructionUnit = txtConstructionUnit.Text;
            instance.EngineeringSite = txtEngineeringSite.Text;
            instance.StrengthGrade = txtStrengthGrade.Text;
            instance.Slump = txtSlump.Text;
            instance._5Stones = txt5Stones.Text;
            instance._31Stones = txt31Stones.Text;
            instance._1Cement = txt1Cement.Text;
            instance._3Cement = txt3Cement.Text;
            instance.Fine = txtFine.Text;
            instance.CoarseSand = txtCoarseSand.Text;
            instance.ASH = txtASH.Text;
            instance._1Admixture = txt1Admixture.Text;
            instance._2Admixture = txt2Admixture.Text;
            instance.Water = txtWater.Text;
            instance.Remark = txtRemark.Text;
            instance.Approval = txtApproval.Text;
            instance.Plan = txtPlan.Text; 

            OperationResult<tb_RPT_MixingProportionOfConcreteOrder> result = RPTMixingProportionOfConcreteOrderServiceObj.CreateMixingProportionOfConcreteOrder(instance);
            BindGridView();

        }
        protected void btnEdit_Click(object sender, EventArgs e)
        {
            int editId = 0;
            if (lbEidtId.Text.Length > 0)
            {
                editId = Int32.Parse(lbEidtId.Text);
            }
            RPTMixingProportionOfConcreteOrderService RPTMixingProportionOfConcreteOrderServiceObj = ServiceFactory.getRPTMixingProportionOfConcreteOrderService();
 
             tb_RPT_MixingProportionOfConcreteOrder entity = new tb_RPT_MixingProportionOfConcreteOrder();

            entity.Id = editId;
            entity.InputDate = txtEditInputDate.Text;
            entity.ProjectName = txtEditProjectName.Text;
            entity.ConstructionUnit = txtEditConstructionUnit.Text;
            entity.EngineeringSite = txtEditEngineeringSite.Text;
            entity.StrengthGrade = txtEditStrengthGrade.Text;
            entity.Slump = txtEditSlump.Text;
            entity._5Stones = txtEdit5Stones.Text;
            entity._31Stones = txtEdit31Stones.Text;
            entity._1Cement = txtEdit1Cement.Text;
            entity._3Cement = txtEdit3Cement.Text;
            entity.Fine = txtEditFine.Text;
            entity.CoarseSand = txtEditCoarseSand.Text;
            entity.ASH = txtEditASH.Text;
            entity._1Admixture = txtEdit1Admixture.Text;
            entity._2Admixture = txtEdit2Admixture.Text;
            entity.Water = txtEditWater.Text;
            entity.Remark = txtEditRemark.Text;
            entity.Approval = txtEditApproval.Text;
            entity.Plan = txtEditPlan.Text; 

            entity.ModifyDate = DateTime.Now;
            entity.ModifyBy = 1;
            entity.CreateDate = DateTime.Now;

            OperationResult<tb_RPT_MixingProportionOfConcreteOrder> resultEdit = RPTMixingProportionOfConcreteOrderServiceObj.ModifyMixingProportionOfConcreteOrder(entity);
            BindGridView();
        }

        protected void btnDelete_Click(object sender, EventArgs e)
        {
            int deleteId = Int32.Parse(lbDeleteId.Text);
            RPTMixingProportionOfConcreteOrderService RPTMixingProportionOfConcreteOrderServiceObj = ServiceFactory.getRPTMixingProportionOfConcreteOrderService();
            tb_RPT_MixingProportionOfConcreteOrder deleteEntity = new tb_RPT_MixingProportionOfConcreteOrder();
            deleteEntity.Id = deleteId;
            RPTMixingProportionOfConcreteOrderServiceObj.DeleteMixingProportionOfConcreteOrder(deleteEntity);

            BindGridView();
        }

        protected void gvRPTMixingProportionOfConcreteList_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName == "Select")
            {
                string strSelectId = e.CommandArgument.ToString();
                BindEdieDialog(Int32.Parse(strSelectId));
            }

            if (e.CommandName == "del")
            {
                string strSelectId = e.CommandArgument.ToString();
                DeleteRecords(Int32.Parse(strSelectId));
            }
        }

        protected void gvRPTMixingProportionOfConcreteList_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            gvRPTMixingProportionOfConcreteList.PageIndex = e.NewPageIndex;
            BindGridView();
        }
        #endregion
    }
}
